package com.quantumoctopus.magiceatball;

import java.util.ArrayList;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class QuestionBuilder {

	private JsonElement fullQuestionJson;

	// given a JsonElement that contains just one question, returns a build
	// Question object
	public ArrayList<Question> getArrayListOfQuestions(String type) {
		ArrayList<Question> qlist = new ArrayList<Question>();
		JsonArray jsa = fullQuestionJson.getAsJsonObject().get("questions")
				.getAsJsonObject().get(type).getAsJsonArray();

		for (int i = 0; i < jsa.size(); i++) {
			Question q = new Question();
			JsonObject json = jsa.get(i).getAsJsonObject();
			String tempString = json.get("text").toString().replace("\"", "");
			q.setText(tempString);

			int answerCount = json.getAsJsonArray("answers").size();
			for (int j = 0; j < answerCount; j++) {
				tempString = json.getAsJsonArray("answers").get(j).toString()
						.replace("\"", "");
				q.addAnswer(tempString);
				tempString = json.getAsJsonArray("answers_effect").get(j)
						.toString().replace("\"", "");
				q.addAnswerMap(q.getAnswer(j), tempString);
			}
			qlist.add(q);
		}
		return qlist;
	}

	/*
	 * public Question getQuestion(JsonElement jse) { Question q = new
	 * Question(); JsonObject json = jse.getAsJsonObject(); tempString =
	 * json.get("text").toString().replace("\"", ""); q.setText(tempString);
	 * 
	 * int answerCount = json.getAsJsonArray("answers").size(); for (int i = 0;
	 * i < answerCount; i++) { tempString =
	 * json.getAsJsonArray("answers").get(i).toString() .replace("\"", "");
	 * q.addAnswer(tempString); tempString =
	 * json.getAsJsonArray("answers_effect").get(i) .toString().replace("\"",
	 * ""); q.addAnswerMap(q.getAnswer(i), tempString); } return q; }
	 * 
	 * // Given the entire question list JsonElement and the type, returns a //
	 * JsonArray of the Questions of that type public void
	 * setQuestionType(String type) { jsa =
	 * fullQuestionJson.getAsJsonObject().get("questions")
	 * .getAsJsonObject().get(type).getAsJsonArray(); }
	 * 
	 * public Question getRandomQuestionFromArray() { Random rand = new
	 * Random();
	 * 
	 * JsonElement jse = jsa.get(rand.nextInt(jsa.size())); Question q = new
	 * Question(); JsonObject json = jse.getAsJsonObject(); tempString =
	 * json.get("text").toString().replace("\"", ""); q.setText(tempString);
	 * 
	 * int answerCount = json.getAsJsonArray("answers").size(); for (int i = 0;
	 * i < answerCount; i++) { tempString =
	 * json.getAsJsonArray("answers").get(i).toString() .replace("\"", "");
	 * q.addAnswer(tempString); tempString =
	 * json.getAsJsonArray("answers_effect").get(i) .toString().replace("\"",
	 * ""); q.addAnswerMap(q.getAnswer(i), tempString); } return q; }
	 * 
	 * public Question getNextQuestionInArray() { JsonElement jse =
	 * jsa.get(index++); Question q = new Question(); JsonObject json =
	 * jse.getAsJsonObject(); tempString =
	 * json.get("text").toString().replace("\"", ""); q.setText(tempString);
	 * 
	 * int answerCount = json.getAsJsonArray("answers").size(); for (int i = 0;
	 * i < answerCount; i++) { tempString =
	 * json.getAsJsonArray("answers").get(i).toString() .replace("\"", "");
	 * q.addAnswer(tempString); tempString =
	 * json.getAsJsonArray("answers_effect").get(i) .toString().replace("\"",
	 * ""); q.addAnswerMap(q.getAnswer(i), tempString); } return q; }
	 */

	public void loadQuestions(String resource) {
		try {
			fullQuestionJson = JSONWorker.getJSONElementFromString(resource);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
